package com.calm.logincenter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Create by @Calm
 *
 * @date 2021/3/5 14:29
 * @note Hook AMS实现集中式登录管理
 **/
public class HookAms {
    private Context mContext;
    public void hookAms(Context context) throws Exception{
        this.mContext = context.getApplicationContext();
        Field field = null;
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
            Class<?> clz = Class.forName("android.app.ActivityManagerNative");
            field = clz.getDeclaredField("gDefault");
        }else if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.P){
            Class<?> clz = Class.forName("android.app.ActivityManager");
            field = clz.getDeclaredField("IActivityManagerSingleton");
        }else {
            Class<?> clz = Class.forName("android.app.ActivityTaskManager");
            field = clz.getDeclaredField("IActivityTaskManagerSingleton");
        }
        field.setAccessible(true);
        Object o = field.get(null);
        Class<?> singleton = Class.forName("android.util.Singleton");
        Field mInstance = singleton.getDeclaredField("mInstance");
        mInstance.setAccessible(true);
        //Android 10在Application中hook会失败，manager返回null
        //hook的时机该如何把握
        Object manager = mInstance.get(o);
        Class<?> iAm = null;
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.P){
            iAm = Class.forName("android.app.IActivityManager");
        }else {
            iAm = Class.forName("android.app.IActivityTaskManager");
        }
        Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class[]{iAm},new AmsInvocationHandler(manager));
        mInstance.set(o,proxy);
    }
    private class AmsInvocationHandler implements InvocationHandler{
        private Object mManager;
        public AmsInvocationHandler(Object o){
            this.mManager = o;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Log.e("CALM",method.getName());
            if(method.getName().equals("startActivity")){
                Intent intent = null;
                for (int i = 0; i < args.length; i++){
                    Object arg = args[i];
                    if(arg instanceof Intent){
                        intent = (Intent) arg;
                        break;
                    }
                }
                SharedPreferences sharedPreferences = mContext.getSharedPreferences("CALM",Context.MODE_PRIVATE);
                //未登录
                if(!sharedPreferences.getBoolean("login",false)){
                    String jumpActivity = intent.getComponent().getClassName();
                    if(!jumpActivity.equals("com.calm.logincenter.LoginActivity")
                        && !jumpActivity.equals("com.calm.logincenter.TowActivity")){
                        intent.putExtra("jumpClass",jumpActivity);
                        ComponentName componentName = new ComponentName(mContext,LoginActivity.class);
                        intent.setComponent(componentName);
                    }
                }
            }
            return method.invoke(mManager,args);
        }
    }
}
